package com.easy.otc.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.easy.otc.dao.ICcpDao;
import com.easy.otc.data.Ccp;


@Transactional
public class CcpDao extends JdbcDaoSupport implements ICcpDao
{
	private String findAllSql;
	private String findAllActive;
	private String findByIdSql;
	private String findByNameSql;
	private String findByKeySql;
	private String insertSql;
	private String updateSql;

	public Collection<Ccp> findAll()
	{
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
		List<Ccp> allCcp = jdbcTemplate.query(findAllSql, new CcpRowMapper());
		return allCcp;
	}

	public Collection<Ccp> findAllActive()
	{
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
		List<Ccp> allCcp = jdbcTemplate.query(findAllSql, new CcpRowMapper());
		return allCcp;
	}

	public Ccp findById(final Long id)
	{
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
		List<Ccp> result = jdbcTemplate.query(findByIdSql, new Object[] { id }, new int[] { Types.BIGINT }, new CcpRowMapper());
		Ccp ccp = DataAccessUtils.singleResult(result);
		return ccp;
	}

	public Ccp findByKey(final String ccpName, final String productType, final String jurisdiction, String clearingModel)
	{
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
		List<Ccp> result = jdbcTemplate.query(findByKeySql, new Object[] { ccpName, productType, jurisdiction, clearingModel },
				new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR }, new CcpRowMapper());
		Ccp ccp = DataAccessUtils.singleResult(result);
		return ccp;
	}

	public void save(Ccp data)
	{
		Long id = data.getCcpId();
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
		if (id != null && findById(id) != null)
		{
			jdbcTemplate.update(updateSql, data.getCcpName(), data.getProductType(), data.getJurisdiction(),
					data.getClearingModel(), data.isInActive(), data.getCcpId());
		}
		else
		{
			Ccp ccpFromDb = findByKey(data.getCcpName(), data.getProductType(), data.getJurisdiction(), data.getClearingModel());
			if (ccpFromDb != null)
			{
				jdbcTemplate.update(updateSql, data.getCcpName(), data.getProductType(), data.getJurisdiction(),
						data.getClearingModel(), data.isInActive(), ccpFromDb.getCcpId());
			}
			else
			{
				jdbcTemplate.update(insertSql, data.getCcpName(), data.getProductType(), data.getJurisdiction(),
						data.getClearingModel(), data.isInActive());
			}
		}
	}

	public void saveAll(Collection<Ccp> data)
	{
		for (Ccp ccp : data)
		{
			save(ccp);
		}
	}

	public Collection<Ccp> findByName(String name)
	{
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
		List<Ccp> allCcp = jdbcTemplate.query(findByNameSql, new CcpRowMapper(), name);
		return allCcp;
	}

	public void setFindAllSql(String findAllSql)
	{
		this.findAllSql = findAllSql;
	}

	public String getFindAllSql()
	{
		return findAllSql;
	}

	public void setFindAllActive(String findAllActive)
	{
		this.findAllActive = findAllActive;
	}

	public String getFindAllActive()
	{
		return findAllActive;
	}

	public String getFindByIdSql()
	{
		return findByIdSql;
	}

	public void setFindByIdSql(String findByIdSql)
	{
		this.findByIdSql = findByIdSql;
	}

	public String getFindByNameSql()
	{
		return findByNameSql;
	}

	public void setFindByNameSql(String findByNameSql)
	{
		this.findByNameSql = findByNameSql;
	}

	public String getInsertSql()
	{
		return insertSql;
	}

	public void setInsertSql(String insertSql)
	{
		this.insertSql = insertSql;
	}

	public String getUpdateSql()
	{
		return updateSql;
	}

	public void setUpdateSql(String updateSql)
	{
		this.updateSql = updateSql;
	}

	public String getFindByKeySql()
	{
		return findByKeySql;
	}

	public void setFindByKeySql(String findByKeySql)
	{
		this.findByKeySql = findByKeySql;
	}
}

class CcpRowMapper implements RowMapper<Ccp>
{

	@Override
	public Ccp mapRow(ResultSet rs, int rowNum) throws SQLException
	{
		Long id = rs.getLong("CcpId");
		String name = rs.getString("CcpName");
		String productType = rs.getString("ProductType");
		String jurisdiction = rs.getString("Jurisdiction");
		String clearingModel = rs.getString("ClearingModel");
		boolean inActive = rs.getBoolean("InActive");
		Ccp ccp = new Ccp(id, name, productType, jurisdiction, clearingModel);
		ccp.setInActive(inActive);
		return ccp;
	}

}
